<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
    <crudOperation :permission="permission">
      <el-button
              slot="right"
              class="filter-item"
              size="mini"
              type="danger"
              icon="el-icon-remove-outline"
              :loading="enable"
              @click="crud.enableOrDisable('DISABLE')"
              :disabled="crud.selections.length < 1"
              v-permission="permission.disable"
      >
        禁用
      </el-button>
      <el-button
              slot="right"
              class="filter-item"
              size="mini"
              type="success"
              icon="el-icon-circle-check"
              :loading="able"
              :disabled="crud.selections.length < 1"
              @click="crud.enableOrDisable('ENABLE')"
              v-permission="permission.enable"
      >
        启用
      </el-button>
    </crudOperation>
      <div v-if="!crud.props.searchToggle">
        <el-form size="small" label-width="70px">
          <el-row :gutter="10">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="客户名称">
                <el-input></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="销售部门">
                <el-input></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="销售人员">
                <el-input></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="客户分类">
                <el-input></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="客户区域">
                <el-input></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <rrOperation :crud="crud"/>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="800px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="开始日期" prop="startDate">
                <el-date-picker
                  v-model="form.startDate"
                  align="right"
                  unlink-panels
                  class="fl1"
                  format="yyyy - MM - dd "
                  value-format="yyyy-MM-dd"
                  default-value
                  range-separator=""
                  placeholder="开始日期"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="结束日期" prop="startDate">
              <el-date-picker
                v-model="form.endDate"
                align="right"
                unlink-panels
                class="fl1"
                format="yyyy - MM - dd "
                value-format="yyyy-MM-dd"
                default-value
                range-separator=""
                placeholder="结束日期"
                style="width: 100%"
              />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
            <el-form-item label="编码">
              <el-input v-model="form.code" />
            </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="名称">
              <el-input v-model="form.name" />
            </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="业务日期">
              <el-input v-model="form.busDate" />
            </el-form-item>
            </el-col>
              <el-col :span="12">
                <el-form-item label="状态">
                  <el-input v-model="form.status" />
                </el-form-item>
              </el-col>
          </el-row>
          <el-row>
          <el-form-item label="备注">
            <el-input v-model="form.remark" />
          </el-form-item>
          </el-row>
          <el-row>
          <el-form-item label="活动信息">
            <div class="flex flex-reverse"  style="margin-bottom: 10px">
              <el-button type="success" @click="addrule">添加</el-button>
            </div>

            <div v-for="(item,index) in form.ruleList"  :key="index" style="margin-bottom: 10px" class="flex">
              满<el-input  v-model="item.full"/>赠<el-input v-model="item.result"/>
            </div>
          </el-form-item>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" stripe border v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" width="55" label="序号"/>
        <el-table-column  prop="code" label="编码" />
        <el-table-column  prop="name" label="名称" />
        <el-table-column  prop="type" label="类型(1:充值)" />
        <el-table-column  prop="busDate" label="业务日期">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.busDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('startDate')" prop="startDate" label="开始日期">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('endDate')" prop="endDate" label="结束日期">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="level" label="优先级" />
        <el-table-column prop="status" label="状态(0禁用 1启用)" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column v-permission="['admin','activeCustomerRecharge:edit','activeCustomerRecharge:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudActiveCustomerRecharge from '@/api/erp/activeCustomerRecharge'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { activeCustomerRechargeDetail } from '../../../api/erp/activeCustomerCouponRecord'

// crud交由presenter持有
const defaultCrud = CRUD({ title: '客户充值活动', url: 'api/activeCustomerRecharge', sort: 'id,desc', crudMethod: { ...crudActiveCustomerRecharge }})
const defaultForm = {
    id:null,
    code:null,
    name:null,
    busDate:null,
    startDate:null,
    endDate:null,
    level:null,
    status:null,
    remark:null,
    ruleList:[{
    id:null,
    orderId:null,
    full:null,
    result:null,
    seq:null,
    remark:null,
  }]}
export default {
  name: 'ActiveCustomerRecharge',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {

      permission: {
        add: ['admin', 'activeCustomerRecharge:add'],
        edit: ['admin', 'activeCustomerRecharge:edit'],
        del: ['admin', 'activeCustomerRecharge:del'],
        import: ['admin', 'activeCustomerRecharge:import'],
        export: ['admin', 'activeCustomerRecharge:export'],
        copy: ['admin', 'activeCustomerRecharge:copy'],
        upload: ['admin', 'activeCustomerRecharge:upload'],
        download: ['admin', 'activeCustomerRecharge:download'],
        enable: ['admin', 'activeCustomerRecharge:enable'],
        disable: ['admin', 'activeCustomerRecharge:disable'],
      },
      rules: {
        startDate: [
          { required: true, message: '开始日期不能为空', trigger: 'blur' }
        ],
        endDate: [
          { required: true, message: '结束日期不能为空', trigger: 'blur' }
        ]
      }    }
  },
  watch: {
  },
  methods: {
    [CRUD.HOOK.beforeToAdd]() {
      this.form ={
        id:null,
        code:null,
        name:null,
        busDate:null,
        startDate:null,
        endDate:null,
        level:null,
        status:null,
        remark:null,
        ruleList:[{
          id:null,
          orderId:null,
          full:null,
          result:null,
          seq:null,
          remark:null,
        }]}
      return true
    },
    [CRUD.HOOK.beforeSubmit]() {
      this.sort()
      this.form.ruleList.forEach((item,index)=>{
        this.form.ruleList[index].seq = index
      })
      this.crud.form = this.form
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.beforeToEdit](crud, form) {
      activeCustomerRechargeDetail({id:form.id}).then(res=>{
        this.form = res.data.content[0]
        this.sort()})
    },
    sort(){
      this.form.ruleList.sort((a,b)=>a.full-b.full)
    },
    addrule(){
      this.form.ruleList.push({
        full:null,
        result:null,
        seq:null,
        remark:null,
      })
    }
  }
}
</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;

  }
</style>
